package com.woyou.model;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import org.androidannotations.annotations.AfterInject;
import org.androidannotations.annotations.EBean;
import org.androidannotations.annotations.EBean.Scope;

import retrofit.RetrofitError;

import com.woyou.bean.ActivityType;
import com.woyou.bean.CodeListResult;
import com.woyou.bean.CodeResult;
import com.woyou.bean.Comments;
import com.woyou.bean.CommentsCon;
import com.woyou.bean.Goods;
import com.woyou.bean.GoodsType;
import com.woyou.bean.Ladder;
import com.woyou.bean.Option;
import com.woyou.bean.OptionGroup;
import com.woyou.bean.PointList;
import com.woyou.bean.Result;
import com.woyou.bean.ShopDetail;
import com.woyou.bean.ShopItem;
import com.woyou.bean.ShopType;
import com.woyou.bean.SubGoods;
import com.woyou.bean.Support;
import com.woyou.bean.rpc.CommentsReq;
import com.woyou.bean.rpc.GoodsListReq;
import com.woyou.bean.rpc.LatLngReq;
import com.woyou.bean.rpc.MD5Req;
import com.woyou.bean.rpc.QueryRadiusReq;
import com.woyou.bean.rpc.QueryRadiusRes;
import com.woyou.bean.rpc.ShopCollectReq;
import com.woyou.bean.rpc.ShopDetailReq;
import com.woyou.bean.rpc.ShopListReq;
import com.woyou.bean.rpc.ShopTypeReq;
import com.woyou.model.rpc.ShopService;

/**
 * 店铺相关业务逻辑封装
 * 
 * @author longtao.li
 * 
 * @param <T>
 * @param <E>
 */
@SuppressWarnings({"rawtypes", "unused"})
@EBean(scope = Scope.Singleton)
public class ShopsModel extends SuperModel {

	private ShopService mShopService;

	@AfterInject
	// 在构造函数调用之后执行
	@Override
	public void onInit() {
		mShopService = mNeWrapper.getNetService(ShopService.class);
	}

	/**
	 * 查询店铺列表
	 * 
	 * @param shopList
	 * @return
	 */
	public CodeListResult<ShopItem> v2_3queryShopList(ShopListReq shopList)
			throws RetrofitError {
		MD5Req<ShopListReq> md5Req = new MD5Req<ShopListReq>(shopList);
		CodeListResult<ShopItem> result = mShopService.v2_3queryShopList(
				md5Req.jsonParams, md5Req.isEncrypted, md5Req.timeStamp,
				md5Req.randomNum, md5Req.sign);
		return result;
	}
	
	/**
	 * 2.26	获取平台活动列表（http://xxxx/queryActTypeList）
	 * @param latLngReq
	 * @return
	 * @throws RetrofitError
	 */
	public CodeListResult<ActivityType> queryActTypeList(LatLngReq latLngReq)
			throws RetrofitError {
		MD5Req<LatLngReq> md5Req = new MD5Req<LatLngReq>(latLngReq);
		CodeListResult<ActivityType> result = mShopService.queryActTypeList(
				md5Req.jsonParams, md5Req.isEncrypted, md5Req.timeStamp,
				md5Req.randomNum, md5Req.sign);
		return result;
	}

	/**
	 * 2.10	查询评论列表（http://xxxx/ v2_1queryComments）
	 * 
	 * @param commentsReq
	 * @return
	 * @throws RetrofitError
	 */
	public Result<Comments> v2_1queryComments(CommentsReq commentsReq)
			throws RetrofitError {
		MD5Req<CommentsReq> md5Req = new MD5Req<CommentsReq>(commentsReq);
		Result<Comments> result = mShopService.v2_1queryComments(
				md5Req.jsonParams, md5Req.isEncrypted, md5Req.timeStamp,
				md5Req.randomNum, md5Req.sign);
//		CodeResult<Comments> result = mockQueryComments();
		return result;
	}

	/**
	 * 2.8	获取店铺详情（http://xxxx/ v2_3queryShopInfo）
	 * @param shopDetailReq
	 * @return
	 * @throws RetrofitError
	 */
	public Result<ShopDetail> v2_3queryShopInfo(ShopDetailReq shopDetailReq)
			throws RetrofitError {
		MD5Req<ShopDetailReq> md5Req = new MD5Req<ShopDetailReq>(shopDetailReq);
		Result<ShopDetail> result = mShopService.v2_3queryShopInfo(
				md5Req.jsonParams, md5Req.isEncrypted, md5Req.timeStamp,
				md5Req.randomNum, md5Req.sign);
//		Result<ShopDetail> result = mockQueryShopInfo();
		return result;
	}

	/**
	 * 获取店铺商品列表
	 * 
	 * @param sId
	 * @param sType
	 * @param page
	 * @return
	 * @throws RetrofitError
	 */
	public Result<List<Goods>> v2_2queryGoodsList(GoodsListReq goodsListReq)
			throws RetrofitError {
		MD5Req<GoodsListReq> md5Req = new MD5Req<GoodsListReq>(goodsListReq);
		Result<List<Goods>> result = mShopService.v2_2queryGoodsList(
				md5Req.jsonParams, md5Req.isEncrypted, md5Req.timeStamp,
				md5Req.randomNum, md5Req.sign);
//		 CodeListResult<Goods> result = mockQueryGoodsList();
		return result;
	}

	/**
	 * 查询店铺类型列表
	 * 
	 * @param req
	 * @return
	 * @throws RetrofitError
	 */
	public CodeListResult<ShopType> queryShopType(ShopTypeReq shopTypeReq)
			throws RetrofitError {
		MD5Req<ShopTypeReq> md5Req = new MD5Req<ShopTypeReq>(shopTypeReq);
		CodeListResult<ShopType> result = mShopService.queryShopType(
				md5Req.jsonParams, md5Req.isEncrypted, md5Req.timeStamp,
				md5Req.randomNum, md5Req.sign);
		// CodeListResult<ShopType> result = mockQueryShopType();
		return result;
	}
	
	/**
	 * 2.20	收藏/取消收藏店铺（http://xxxx/shopCollect）
	 * @param shopCollectReq
	 * @return
	 * @throws RetrofitError
	 */
	public CodeResult shopCollect(ShopCollectReq shopCollectReq)
			throws RetrofitError {
		MD5Req<ShopCollectReq> md5Req = new MD5Req<ShopCollectReq>(
				shopCollectReq);
		CodeResult result = mShopService.shopCollect(md5Req.jsonParams,
				md5Req.isEncrypted, md5Req.timeStamp, md5Req.randomNum,
				md5Req.sign);
		return result;
	}

	/**
	 * 2.2	查询店铺配送范围（http://xxxx/ v2_1query Radius）
	 * @return
	 */
	public Result<List<Ladder>> v2_1queryRadius(QueryRadiusReq queryRadiusReq) throws RetrofitError {
			MD5Req<QueryRadiusReq> md5Req = new MD5Req<QueryRadiusReq>(queryRadiusReq);
			Result<List<Ladder>> result = mShopService.v2_1queryRadius(md5Req.jsonParams,
					md5Req.isEncrypted, md5Req.timeStamp, md5Req.randomNum,
					md5Req.sign);
//			Result<QueryRadiusRes> result=mockv2_1queryRadius();
			return result;
		}

	/**
	 * 获取店铺关键字历史记录
	 * 
	 * @return
	 */
	public List<String> getSearchKeys() {
		List<String> keys = mSearchKeyDao.getKeys(mContext);
		return keys;
	}

	/**
	 * 更新关键字历史记录
	 * 
	 * @param keys
	 */
	public void updateKeys(List<String> keys) {
		mSearchKeyDao.putKeys(mContext, keys);
	}
	
	/**
	 * 清空关键字历史记录
	 */
	public void clearKeys() {
		mSearchKeyDao.clearKeys(mContext);
	}
	/**
	 * 查询外送范围
	 * @return
	 */
	private Result<QueryRadiusRes> mockv2_1queryRadius(){
		Result<QueryRadiusRes> res=new Result<QueryRadiusRes>();
		res.code=1;
		res.msg="成功";
		QueryRadiusRes queryRadiusRes=new QueryRadiusRes();
		List<Ladder> ladders=new ArrayList<Ladder>();
		for(int i=1;i<4;i++){
			Ladder ladder=new Ladder();
			ladder.setlId("iId");
			ladder.setRadius(500*i);
			ladder.setSendFee(20*i);
			ladder.setShapeType(1);
			ladder.setSpending(10*i);
			PointList pointList=new PointList();
			pointList.setLat(39.90403f);
			pointList.setLng(116.407525f);
			ladders.add(ladder);
		}
		queryRadiusRes.setLadder(ladders);
		res.data=queryRadiusRes;
		return res;
	}
	
	// queryShopList 的假数据
	int tempPage = 1;
	private CodeListResult<ShopItem> mockQueryShopList() {
		CodeListResult<ShopItem> res = new CodeListResult<ShopItem>();
		res.code = 1;
		res.msg = "成功";
		res.page = ++tempPage;
		List<ShopItem> list = new ArrayList<ShopItem>();
		for (int i = 0; i < 5; i++) {
			ShopItem item = new ShopItem();
			item.setsId("SHD01909");
			item.setsName("天下第一家");
			item.setPicUrl("http://a.hiphotos.baidu.com/image/pic/item/d833c895d143ad4bc15daafa81025aafa40f069c.jpg");
			item.setAddress("杨浦区淞沪路316号");
			item.setAvgReceive("30");
			item.setAvgSend("20");
			item.setDistance("300");
			item.setLat("31.30687800");
			item.setLng("121.51326000");
			item.setNotice("满50减50");
			item.setPhone("110");
			item.setSalesQty("200");
			item.setScore(4.50f);
			item.setCommentsNum("30");
			item.setSpending(20.10f);
			item.setState(1);
			list.add(item);
		}
		for (int i = 0; i < 7; i++) {
			ShopItem item = new ShopItem();
			item.setsId("SHP20151025");
			item.setsName("龙涛大排档");
			item.setPicUrl("http://c.hiphotos.baidu.com/image/pic/item/c83d70cf3bc79f3dca9c5f41bba1cd11728b2945.jpg");
			item.setAddress("杨浦区淞沪路388号政通路");
			item.setAvgReceive("45");
			item.setAvgSend("36");
			item.setDistance("800");
			item.setLat("31.30687800");
			item.setLng("121.51326000");
			item.setNotice("满100减20");
			item.setPhone("13115689562");
			item.setSalesQty("320");
			item.setScore(3.50f);
			item.setCommentsNum("88");
			item.setSpending(12.50f);
			item.setState(2);
			list.add(item);
		}
		for (int i = 0; i < 5; i++) {
			ShopItem item = new ShopItem();
			item.setsId("SH01909");
			item.setsName("小星星水果店");
			item.setPicUrl("http://daili.ideliver.cn/upload/635497558213693037_SH0190982846462.JPG");
			item.setAddress("创智天地广场7号楼602室");
			item.setAvgReceive("55");
			item.setAvgSend("38");
			item.setDistance("1120");
			item.setLat("31.30687800");
			item.setLng("121.51326000");
			item.setNotice("满1000减300,满100减30");
			item.setPhone("15869563195");
			item.setSalesQty("560");
			item.setScore(4.50f);
			item.setCommentsNum("122");
			item.setSpending(26.80f);
			item.setState(3);
			list.add(item);
		}
		res.list = list;
		return res;
	}

	// queryComments 的假数据
	private CodeResult<Comments> mockQueryComments() {
		CodeResult<Comments> result = new CodeResult<Comments>();
		result.code = 1;
		result.msg = "成功";
		Comments item = new Comments();
		item.setPage(tempPage++);
		item.setAllNum(30);
		item.setGoodNum(13);
		item.setMidNum(14);
		item.setBadNum(3);
		item.setPage(1);
		CommentsCon[] commentsCons = new CommentsCon[30];
		CommentsCon commentsCon = null;
		commentsCon = new CommentsCon();
		commentsCon.setcId("001");
		commentsCon
				.setuPic("http://ww3.sinaimg.cn/bmiddle/8b62b930gw1eo8ao42bdmj20c80ic3zr.jpg");
		commentsCon.setuName("传智天地7号楼602室");
		commentsCon.setuLevel(2);
		commentsCon.setScore(1.00f);
		commentsCon.setDeliveryTime("76");
		commentsCon.setcTime("2015-3-12");
		commentsCon.setComments("东西不好吃啊，我们要砸场子怎么办...");
		commentsCons[0] = commentsCon;
		for (int i = 1; i < 30; i++) {
			commentsCon = new CommentsCon();
			commentsCon.setcId("3" + i);
			commentsCon
					.setuPic("http://my.csdn.net/uploads/avatar/D/0/0/1_u012301841.jpg");
			commentsCon.setuName("杨浦区淞沪路316号");
			commentsCon.setuLevel(3);
			commentsCon.setScore(4.50f);
			commentsCon.setDeliveryTime("38");
			commentsCon.setcTime("2014-12-31");
			commentsCon.setComments("菜品很不错，速度也挺快，没有饮料可选这点需要优化!");
			commentsCon.setReComments("你们在黑我们店，从现在开始免费开吃了!");
			commentsCon.setRetime("2011-11-20 20:58");
			commentsCons[i] = commentsCon;
		}
		item.setList(commentsCons);
		result.bean = item;
		return result;
	}

	// queryShopInfo 的假数据
	private Result<ShopDetail> mockQueryShopInfo() {
		Result<ShopDetail> res = new Result<ShopDetail>();
		res.code = 1;
		res.msg = "成功";
		
		ShopDetail shopDetail = new ShopDetail();
		shopDetail.setHours("7:00-12:00 14：20-15：45 16:00-21:00");
		shopDetail.setsId("45");
		shopDetail.setsName("鸿记便当");
		shopDetail.setScore((float) 2.5);
		shopDetail.setCommentsNum("66");
		shopDetail.setMonthSales("777");
		shopDetail.setAddr("杨浦区淞沪路388号创智天地7号楼602室");
		shopDetail.setPhone("18616742809");
		shopDetail.setInfo("匡风含情,你诚实又善良，潇洒而又风趣，你的那些乐于助人、拾金不昧的缺点是应该改正的时候了，加油！帅气的小风");
		shopDetail.setNotice("帅气一直困扰着我！");
		// 店铺支持
		Support support1 = new Support();
		support1.setPicUrl("http://img60.aili.com/1a/15/1a15c6b1346fc738f8f94ec6abe53c39.png");
		support1.setSuppor("新店开张、欢迎光临");
		Support support2 = new Support();
		support2.setPicUrl("http://img10.360buyimg.com/n1/jfs/t277/240/1371969586/310595/e214ffc0/5438864aNad72c6d5.jpg");
		support2.setSuppor("满100送女友一个");
		List<Support> supportList = new ArrayList<Support>();
		supportList.add(support1);
		supportList.add(support2);
		shopDetail.setSupportList(supportList);

		shopDetail.setAvgReceive("25");
		shopDetail.setAvgSend("33");
		shopDetail.setAvgDeliver("45");
		shopDetail.setSendFee(5);
		shopDetail.setToSendPrice(20);
		shopDetail.setScopeList(new String[] { "送货超快", "服务员超美", "吃的超爽","环境没得说", "我们都是好好好好的人啊" });

		// 商品类型
		GoodsType good1 = new GoodsType();
		good1.settId("1");
		good1.setNum("5");
		good1.settName("热菜");
		GoodsType good2 = new GoodsType();
		good2.settId("12");
		good2.setNum("52");
		good2.settName("套餐");
		GoodsType good3 = new GoodsType();
		good3.settId("3");
		good3.setNum("11");
		good3.settName("外卖我有");
		GoodsType good4 = new GoodsType();
		good4.settId("4");
		good4.setNum("22");
		good4.settName("凉菜");
		List<GoodsType> goodTypeList = new ArrayList<GoodsType>();
		goodTypeList.add(good1);
		goodTypeList.add(good2);
		goodTypeList.add(good3);
		goodTypeList.add(good4);
		shopDetail.setGoodsTypeList(goodTypeList);

		res.data = shopDetail;
		return res;
	}

	// queryGoodsList 的假数据
	private CodeListResult<Goods> mockQueryGoodsList() {
		CodeListResult<Goods> res = new CodeListResult<Goods>();
		res.code = 1;
		res.msg = "成功";
		List<Goods> list = new ArrayList<Goods>();
		for (int i = 0; i < 30; i++) {
			Goods goods = new Goods();
			goods.setgId("GL00000" + i);
			goods.setPicUrl("http://daili.ideliver.cn/upload/635497558213693037_SH0190982846462.JPG");
			goods.setgName("小葱拌豆腐");
			goods.setPrice(15.02f);
			goods.setUnit("份");
			goods.setState(1);
			goods.setSaleNum(221);
			goods.setSalesUrl("http://daili.ideliver.cn/upload/635497558213693037_SH0190982846462.JPG");
			goods.setDetailPic("http://daili.ideliver.cn/upload/635497558213693037_SH0190982846462.JPG");

			if( i ==0 ){
				list.add(goods);
				continue;
			}
			// 添加固定套餐明细
			ArrayList<String> detailList;
			if (i %2== 0) {
				detailList = new ArrayList<String>();
				detailList.add("香辣鸡腿堡1个");
				detailList.add("老北京鸡肉卷1个");
				detailList.add("香辣小鸡腿1个");
				detailList.add("奥尔良烤鸡腿堡1个");
				detailList.add("可乐2杯");
				goods.setDetailList(detailList);
			}

			// 添加规格组
			OptionGroup specGroup;
			if (i % 2 == 0) {
				specGroup = new OptionGroup();
				specGroup.setOptGId("123456" + i);
				specGroup.setOptGName("规格");
				specGroup.setRequired(true);
				specGroup.setSelectNum(1);
				ArrayList<Option> optionList = new ArrayList<Option>();

				Option specOption1 = new Option();
				specOption1.setOptId("654321" + i);
				specOption1.setState(1);
				specOption1.setPrice(10);
				specOption1.setOptName("大杯");
				optionList.add(specOption1);

				Option specOption = new Option();
				specOption.setOptId("1234567" + i);
				specOption.setState(1);
				specOption.setPrice(0);
				specOption.setOptName("小杯");
				optionList.add(specOption);

				specGroup.setOptList(optionList);
				goods.setSpecGroup(specGroup);

			}

			// 添加选项组
			ArrayList<OptionGroup> optGList = new ArrayList<OptionGroup>();
			for (int k = 1; k < 5; k++) {
				OptionGroup optionGroup = new OptionGroup();
				optionGroup.setOptGId("123" + k + i);
				optionGroup.setOptGName("套餐");
				optionGroup.setRequired(true);
				optionGroup.setSelectNum(k);
				ArrayList<Option> optionList2 = new ArrayList<Option>();
				Option option = new Option();
				option.setOptId("123456" + k + i);
				option.setState(1);
				option.setPrice(10);
				option.setOptName("辣");
				optionList2.add(option);

				Option option2 = new Option();
				option2.setOptId("654321" + k + i);
				option2.setState(1);
				option2.setPrice(10);
				option2.setOptName("不辣");
				optionList2.add(option2);

				Option option3 = new Option();
				option3.setOptId("89757" + k + i);
				option3.setState(1);
				option3.setPrice(10);
				option3.setOptName("甜");
				optionList2.add(option3);

				Option option4 = new Option();
				option4.setOptId("89757" + k + i);
				option4.setState(1);
				option4.setPrice(10);
				option4.setOptName("甜不辣");
				optionList2.add(option4);

				optionGroup.setOptList(optionList2);
				optGList.add(optionGroup);
			}
			goods.setOptGList(optGList);

			// 添加子商品
			ArrayList<SubGoods> subGoodsList = new ArrayList<SubGoods>();
			int random = new Random().nextInt(5);
			for (int j = 0; j < random; j++) {
				SubGoods subGoods = new SubGoods();
				subGoods.setgId("subGoods" + j + i);
				subGoods.setgName("子商品" + j);
				subGoods.setPrice(random * 2);
				subGoods.setPicUrl("http://daili.ideliver.cn/upload/635497558213693037_SH0190982846462.JPG");
				subGoodsList.add(subGoods);
			}
			goods.setSubGList(subGoodsList);

			// 子商品是否必须选择
			if (i % 2 == 0) {
				goods.setSubGoodsRequired(true);
			} else {
				goods.setSubGoodsRequired(false);
			}
			list.add(goods);
		}
		res.list = list;
		return res;
	}

	// queryShopType 的假数据
	private CodeListResult<ShopType> mockQueryShopType() {
		CodeListResult<ShopType> res = new CodeListResult<ShopType>();
		res.code = 1;
		res.msg = "成功";
		List<ShopType> list = new ArrayList<ShopType>();
		for (int i = 0; i < 10; i++) {
			ShopType shopType = new ShopType();
			shopType.setsTypeId("001");
			shopType.setsTypeName("中式快餐");
			shopType.setNum("30");
			list.add(shopType);
		}
		res.list = list;
		return res;
	}

}
